package com.ruoyi.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.teaching.domain.PlatformCSpecialty;
import com.ruoyi.teaching.domain.vo.PlatformCSpecialtyNodeVO;
import com.ruoyi.teaching.mapper.PlatformCSpecialtyMapper;
import com.ruoyi.teaching.service.PlatformCSpecialtyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王旭
 * @since 2024-06-13
 */
@Service
public class PlatformCSpecialtyServiceImpl extends ServiceImpl<PlatformCSpecialtyMapper, PlatformCSpecialty> implements PlatformCSpecialtyService {

    @Autowired
    private PlatformCSpecialtyMapper platformCSpecialtyMapper;

    @Override
    public AjaxResult selectPlatformCSpecialtyListTree() {
        // 获取所有数据
        List<PlatformCSpecialty> specialties = platformCSpecialtyMapper.selectList(new QueryWrapper<>());

        // 构建以父类ID为键的数据映射
        Map<Integer, List<PlatformCSpecialty>> specialtyMap = specialties.stream()
                .collect(Collectors.groupingBy(PlatformCSpecialty::getParentId));

        // 构建树形结构
        List<PlatformCSpecialtyNodeVO> tree = buildTree(specialtyMap, 0);

        return AjaxResult.success(tree);
    }

    private List<PlatformCSpecialtyNodeVO> buildTree(Map<Integer, List<PlatformCSpecialty>> specialtyMap, int parentId) {
        List<PlatformCSpecialtyNodeVO> nodes = new ArrayList<>();
        List<PlatformCSpecialty> specialties = specialtyMap.get(parentId);

        if (specialties != null) {
            for (PlatformCSpecialty specialty : specialties) {
                PlatformCSpecialtyNodeVO node = new PlatformCSpecialtyNodeVO();
                node.setId(specialty.getId());
                node.setParentId(specialty.getParentId());
                node.setSpecialtyName(specialty.getSpecialtyName());
                node.setChildren(buildTree(specialtyMap, specialty.getId()));  // 递归构建子节点

                nodes.add(node);
            }
        }

        return nodes;
    }

    @Override
    public AjaxResult selectPlatformCSpecialtyList() {
        // 获取所有数据
        List<PlatformCSpecialty> specialties = platformCSpecialtyMapper.selectList(new QueryWrapper<>());

        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",specialties);
    }

    @Override
    public AjaxResult selectPlatformCSpecialtyListTop() {
        // 获取所有数据
        List<PlatformCSpecialty> specialties = platformCSpecialtyMapper.selectList(new QueryWrapper<PlatformCSpecialty>().eq("parent_id",0));

        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",specialties);
    }

    @Override
    public AjaxResult getSpecialtyById(Integer id) {
        PlatformCSpecialty platformCSpecialty = platformCSpecialtyMapper.selectById(id);
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",platformCSpecialty);
    }

    @Override
    public AjaxResult getByBigId(Integer id) {
        QueryWrapper<PlatformCSpecialty> specialtyQueryWrapper = new QueryWrapper<>();
        specialtyQueryWrapper.eq("parent_id",id);
        List<PlatformCSpecialty> platformCSpecialties = platformCSpecialtyMapper.selectList(specialtyQueryWrapper);
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",platformCSpecialties);
    }
}
